// 2025/10/11: 首个版本

using System;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Controls.Primitives;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class SingleSelect : DialogPanel
    {
        public SingleSelect() // For designer
        {
            InitializeComponent();
        }

        public SingleSelect(string message, string[] options, int defaultSelectIndex, String iconResourceName = null)
        {
            InitializeComponent();
            Icon = ResourceLoader.Load(iconResourceName)?.ToAvaloniaWindowIcon();
            Title = message;
            SelectedIndex = defaultSelectIndex;

            int height = 400;
            String rowDef;
            if (options.Length > 8)
            {
                scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
                rowDef = "40";
            }
            else
            {
                height = options.Length * 50;
                scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
                rowDef = "*";
            }
            SetFixMode(400, height, true);

            var definitionString = rowDef;
            for (int i = 0; i < options.Length - 1; i++)
            {
                definitionString += ",8," + rowDef;
            }
            grid.RowDefinitions = new RowDefinitions(definitionString);

            for (int i = 0; i < options.Length; i++)
            {
                var target = i;
                var button = new Button{ Content = options[i], HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center };
                if (i == defaultSelectIndex) button.Background = App.DarkTheme ? Brushes.Green : Brushes.PaleGreen;
                button.Click += delegate
                {
                    SelectedIndex = target;
                    Close();
                };
                Grid.SetRow(button, 2 * i);
                grid.Children.Add(button);
            }
        }

        public int SelectedIndex { get; private set; }
    }
}